package com.example.demo.common;


import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class SecurityUtil {
    public  static  String encrypt(String password){
        String salt= UUID.randomUUID().toString().replace("-","");
        String finalPassword= DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return salt +finalPassword;
    }
    public  static  boolean decrypt(String password,String finalPassword){
        if(!StringUtils.hasLength(password)||!StringUtils.hasLength(finalPassword)){
            return  false;
        }
        if(finalPassword.length()!=64){
            return  false;
        }
        String salt=finalPassword.substring(0,32);
        String securityPassword=DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return (salt+securityPassword).equals(finalPassword);
    }
}


